package com.tingyuge.thinking.spring.dependency.injection;

import org.springframework.beans.BeansException;
import org.springframework.beans.factory.Aware;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.BeanFactoryAware;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;

/**基于aware {@link Aware} 接口回调的依赖注入
 * @author xiaodao
 * @version 1.0
 * @date 2021/6/29
 */
public class AwareInterfaceDependencyFieldInjectionDemo implements BeanFactoryAware , ApplicationContextAware {

    private static BeanFactory beanFactory;
    private static ApplicationContext applicationContext;

    public static void main(String[] args) {
        AnnotationConfigApplicationContext applicationContext = new AnnotationConfigApplicationContext();
        //注册 configuration class（配置类）--->也是 spring bean
        applicationContext.register(AwareInterfaceDependencyFieldInjectionDemo.class);

        //启动应用上下文
        applicationContext.refresh();
        System.out.println(applicationContext == AwareInterfaceDependencyFieldInjectionDemo.applicationContext);

        //显示关闭应用上下文
        applicationContext.close();
    }

    @Override
    public void setBeanFactory(BeanFactory beanFactory) throws BeansException {
        AwareInterfaceDependencyFieldInjectionDemo.beanFactory = beanFactory;
    }

    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        AwareInterfaceDependencyFieldInjectionDemo.applicationContext = applicationContext;
    }
}
